/*
XPTracker - Alternative Agile Tracking Tool
Copyright (C) 2006-2009  Stephen Starkey

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package org.xptracker.web;

import com.google.inject.Inject;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.validation.SimpleError;
import org.xptracker.domain.CurrentUserProvider;
import org.xptracker.domain.Domain;
import org.xptracker.domain.User;
import org.xptracker.persistence.PersistenceManager;

import javax.servlet.http.HttpServletRequest;

public class StandardActionBean implements ActionBean {
  private ActionBeanContext context;

  @Inject
  protected PersistenceManager persistenceManager;

  @Inject
  protected Domain domain;

  @SuppressWarnings({"UnusedDeclaration"})
  @Inject
  protected CurrentUserProvider currentUserProvider;

  public void setContext(ActionBeanContext context) {
    this.context = context;
  }

  public ActionBeanContext getContext() {
    return context;
  }

  /**
   * Use Stripes binding whenever possible.  This method is marked deprecated to warn folks not to use it,
   * but it will never really go away as some scenarios really do require it.
   *
   * @param name name of the parameter to look up
   * @return The value on the request for the given key
   */
  @Deprecated
  protected String param(String name) {
    return request().getParameter(name);
  }

  /**
   * Use Stripes binding whenever possible.  This method is marked deprecated to warn folks not to use it,
   * but it will never really go away as some scenarios really do require it.
   *
   * @param name name of the parameter to look up
   * @return The values on the request for the given key
   */
  @Deprecated
  protected String[] paramValues(String name) {
    return request().getParameterValues(name);
  }

  /**
   * Use Stripes binding whenever possible.  This method is marked deprecated to warn folks not to use it,
   * but it will never really go away as some scenarios really do require it.
   * @return The current request
   */
  @Deprecated
  protected HttpServletRequest request() {
    return getContext().getRequest();
  }

  protected User getCurrentUser() {
    return currentUserProvider.getCurrentUser();
  }

  protected <T> T loadEntity(Class<? extends T> entityType, String uuid) {
    return domain.loadEntity(entityType, uuid);
  }

  protected SimpleError alreadyExistsError(String objectType, String value) {
     return new SimpleError("{2} ''{1}'' already exists.", value, objectType);
  }

  protected void addMessage(String message, Object... args) {
    context.getMessages().add(new SimpleMessage(message, args));
  }

  protected void addError(String field, String message, Object... args) {
    context.getValidationErrors().add(field, new SimpleError(message, args));
  }

  protected void addGlobalError(String message, Object... args) {
    context.getValidationErrors().addGlobalError(new SimpleError(message, args));
  }
}
